﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZLCMS.Business.DwzFrame;
using NHibernate;
using NHibernate.Criterion;
using ZLCMS.Business;
using ZLCMS.Data.Model;

namespace ZLCMS.Web.SystemManage.Basics.Doc
{
    public partial class FileList : System.Web.UI.Page
    {
        protected FrameHelper _framePage;

        protected void Page_Load(object sender, EventArgs e)
        {
            _framePage = new FrameHelper(this) { RecordCount = GetFileCount() };
            if (_framePage.IsAjaxPagerPostBack)
            {
                BindRepeator(_framePage.PageIndex, _framePage.PageSize);
            }
            else if (IsPostBack)
            {
                BindRepeator(1, 20);
            }
            else if (!IsPostBack)
            {
                BindRepeator(1, 20);
            }
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        /// <returns></returns>
        protected int GetFileCount()
        {
            using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
            {
                ICriteria crt = session.CreateCriteria<SysFileInfo>();
                SetupCriteria(ref crt);
                return (int)crt.SetProjection(Projections.RowCount()).UniqueResult();
            }
        }

        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="pageIndex">分页索引</param>
        /// <param name="pageSize">分页大小</param>
        protected void BindRepeator(int pageIndex, int pageSize)
        {
            using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
            {
                ICriteria crt = session.CreateCriteria<SysFileInfo>();
                SetupCriteria(ref crt);
                crt.SetFirstResult((pageIndex - 1) * pageSize).SetMaxResults(pageSize);
                repFile.DataSource = crt.AddOrder(Order.Desc("UploadTime")).List();
                repFile.DataBind();
            }
        }

        protected void SetupCriteria(ref ICriteria crt)
        {
            if (IsPostBack && !String.IsNullOrEmpty(txtFileName.Text))
            {
                crt.Add(Expression.Like("FileName", "%" + txtFileName.Text + "%"));
            }
        }

        protected String GetSizeString(int size)
        {
            if (size >= 1024 * 1024 * 1024) //文件大小大于或等于1024MB   
            {
                return string.Format("{0:0.00} GB", (double)size / (1024 * 1024 * 1024));
            }
            else if (size >= 1024 * 1024) //文件大小大于或等于1024KB   
            {
                return string.Format("{0:0.00} MB", (double)size / (1024 * 1024));
            }
            else if (size >= 1024) //文件大小大于等于1024bytes   
            {
                return string.Format("{0:0.00} KB", (double)size / 1024);
            }
            else
            {
                return string.Format("{0:0.00} bytes", size);
            } 
        }

        protected String GetFileSpanClass(String file)
        {
            String ext = file.Substring(file.LastIndexOf('.') + 1).ToLower();
            return "ext_" + ext;
        }

        protected String GetPreviewAnchor(String name, String path)
        {
            String ext = path.Substring(path.LastIndexOf('.') + 1).ToLower();
            String imgs = "jpg,jpeg,gif,png";
            if(imgs.IndexOf(ext) != -1)
            {
                return String.Format(@"<a href=""{0}"" class=""prettyPhoto"" title=""{1}"">预览</a>", path, name);
            }
            else
            {
                return @"<a style=""color: #C0C0C0"">预览</a>";
            }
        }

    }
}
